package com.paipaideli.ui.mine.safety;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.TStatusView;
import com.paipaideli.common.utils.JsutmentUtil;
import com.paipaideli.common.utils.StringUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.common.utils.preferrnces.FastData;
import com.paipaideli.ui.login.LoginActivity;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by 修改密码
 */
public class ModifyPwdActivity extends BasePresenterActivity {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.et_modfiy_oldpwd)
    EditText etModfiyOldpwd;
    @BindView(R.id.et_modfiy_onepwd)
    EditText etModfiyOnepwd;
    @BindView(R.id.et_modfiy_twopwd)
    EditText etModfiyTwopwd;
    @BindView(R.id.btn_modify_ok)
    Button btnModifyOk;
    @BindView(R.id.bg_statusview)
    TStatusView bg_statusview;

    @Override
    public int getContentView() {
        return R.layout.activity_modifypwd;
    }

    @Override
    public void initView() {
        unbinder = ButterKnife.bind(this);
        top_title.setText("修改密码");

        top_back.setOnClickListener(o -> {
            finish();
        });

        //修改密码
        RxView.clicks(btnModifyOk).subscribe(o -> {
            modifyPwd();
        }, throwable -> {
        });
    }

    private void modifyPwd() {
        String old = etModfiyOldpwd.getText().toString().trim();
        String onePwd = etModfiyOnepwd.getText().toString().trim();
        String twoPwd = etModfiyTwopwd.getText().toString().trim();
        if (!StringUtil.isEmpty(old)) {
            if (!StringUtil.isEmpty(onePwd)) {
                if (!StringUtil.isEmpty(twoPwd)) {
                    if (onePwd.equals(twoPwd)) {
                        JSONObject requestData = new JSONObject();
                        try {
                            requestData.put("oldPwd", old);
                            requestData.put("pwd", onePwd);
                            requestData.put("pwd1", twoPwd);
                        } catch (Exception e) {
                            e.getMessage();
                        }
                        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
                        bg_statusview.showLoading();
                        Api.getInstanceGson().modifyPwd(body)
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(request -> {
                                    if (request.getCode().equals("200")) {
                                        FastData.setUsername("");
                                        FastData.setUserPwd("");
                                        FastData.setIsLogin(false);
                                        Intent intent = new Intent(ModifyPwdActivity.this, LoginActivity.class);
                                        startActivity(intent);
                                        finish();
                                        bg_statusview.finish();
                                        ToastUtil.show(request.getMessage());
                                    } else {
                                        bg_statusview.finish();
                                        ToastUtil.show(request.getMessage());
                                    }
                                }, throwable -> {
                                    bg_statusview.finish();
                                    Log.e(TAG, "error", throwable);
                                });
                    } else {
                        ToastUtil.show("两次密码输入不一致，请重新输入！");
                    }
                } else {
                    ToastUtil.show("请输入密码！");
                }
            } else {
                ToastUtil.show("请输入密码！");
            }
        } else {
            ToastUtil.show("请输入旧密码！");
        }


    }

}
